package pms.dao.impl;



import pms.dao.OrderDao;
import pms.entity.Medicine;
import pms.entity.Order;
import pms.entity.Supplier;
import pms.until.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    JDBCTools tools=new JDBCTools();
    @Override
    public boolean addMedicineByNecessity(Order order) {
        int rs=tools.update("insert into (order_supplier,order_medicine,order_date,order_number,order_amount,order_status) values(?,?,?,?,?,?)",
                order.getOrder_supplier().getSupplier_id(),order.getOrder_medicine().getMedicine_id(),order.getOrder_date(),order.getOrder_number(),order.getOrder_amount(),order.getOrder_status());
        return rs>0;
    }

    @Override
    public List<Medicine> selectMedicineByNecessity() {
        List<Medicine> medicineList=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_medicine");
        Medicine medicines=null;
        try {
            while(rs.next()){
                medicines=new Medicine();
                medicines.setMedicine_id(rs.getInt("medicine_id"));
                medicines.setMedicine_date(rs.getDate("medicine_id").toLocalDate());
                medicines.setMedicine_name(rs.getString("medicine_name"));
                medicines.setMedicine_number(rs.getInt("medicine_number"));
                medicines.setMedicine_price(rs.getDouble("medicine_price"));
                medicines.setMedicine_thd(rs.getInt("medicine_thd"));
                medicineList.add(medicines);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return medicineList;
    }

    @Override
    public List<Supplier> selectAllSupplier() {
        List<Supplier> supplierList=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_supplier");
        Supplier supplier=null;
        try {
            while(rs.next()){
                supplier=new Supplier();
                supplier.setSupplier_id(rs.getInt("supplier_id"));
                supplier.setSupplier_status(rs.getString("supplier_status"));
                supplier.setSupplier_tel(rs.getString("supplier_tel"));
                supplier.setSupplier_name(rs.getString("supplier_name"));
                supplier.setSupplier_qct(rs.getInt("supplier_qct"));
                supplier.setSupplier_mainMedicine((rs.getInt("supplier_mainMedicine")));
                supplier.setSupplier_price(rs.getInt("supplier_price"));
                supplierList.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            tools.clossAll();
        }
        return supplierList;
    }

    @Override
    public boolean updateSupplierById(Supplier supplier) {
        int rs=tools.update("update t_supplier set supplier_name=?,supplier_qct=?,supplier_tel=?,supplier_status=?,supplier_mainMedicine=?,supplier_price=? where supplier_id=?",
                supplier.getSupplier_name(),supplier.getSupplier_qct(),supplier.getSupplier_tel(),supplier.getSupplier_status(),supplier.getSupplier_mainMedicine(),supplier.getSupplier_id());
        return rs>0;
    }

    @Override
    public boolean judgeIfNeedToPurchaseExtraMedicine(Medicine medicine) {
        if(medicine.getMedicine_number()<medicine.getMedicine_thd()){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Supplier selectSupplierById(Supplier supplier) {
        ResultSet rs=tools.query("select * from t_supplier where supplier_id=?",supplier.getSupplier_id());
        Supplier supplier1=null;
        try {
            while(rs.next()){
                supplier1=new Supplier();
                supplier1.setSupplier_id(rs.getInt("supplier_id"));
                supplier1.setSupplier_status(rs.getString("supplier_status"));
                supplier1.setSupplier_tel(rs.getString("supplier_tel"));
                supplier1.setSupplier_name(rs.getString("supplier_name"));
                supplier1.setSupplier_qct(rs.getInt("supplier_qct"));
                supplier1.setSupplier_mainMedicine(rs.getInt("supplier_mainMedicine"));
                supplier1.setSupplier_price(rs.getInt("supplier_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return supplier1;
    }

    @Override
    public Supplier selectSupplierById(Integer supplier_id) {
        ResultSet rs=tools.query("select * from t_supplier where supplier_id=?",supplier_id);
        Supplier supplier1=null;
        try {
            while(rs.next()){
                supplier1=new Supplier();
                supplier1.setSupplier_id(rs.getInt("supplier_id"));
                supplier1.setSupplier_status(rs.getString("supplier_status"));
                supplier1.setSupplier_tel(rs.getString("supplier_tel"));
                supplier1.setSupplier_name(rs.getString("supplier_name"));
                supplier1.setSupplier_qct(rs.getInt("supplier_qct"));
                supplier1.setSupplier_mainMedicine(rs.getInt("supplier_mainMedicine"));
                supplier1.setSupplier_price(rs.getInt("supplier_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return supplier1;
    }

    @Override
    public Supplier compareSupplierToGetBestPrice(Integer medicine_id) {
        Medicine medicine=selectMedicineById(medicine_id);
        List<Supplier> supplierList1=selectAllSupplier();
        List<Supplier> supplierList2=null;
        for(Supplier SameNameSupplier:supplierList1){
            if(medicine.getMedicine_id()==SameNameSupplier.getSupplier_mainMedicine()){
                supplierList2.add(SameNameSupplier);
            }
        }
        Integer LeastPrice=supplierList2.get(0).getSupplier_price();
        for(Supplier LeastPricee:supplierList2){
            if(LeastPricee.getSupplier_price()<=LeastPrice){
                LeastPrice=LeastPricee.getSupplier_price();
            }
        }
        Supplier supplierLeast=new Supplier();
        for(Supplier LeastPriceSupplier1:supplierList2){
            if(LeastPriceSupplier1.getSupplier_price()==LeastPrice);
            supplierLeast=LeastPriceSupplier1;
        }
        return supplierLeast;
    }

    @Override
    public Medicine selectMedicineById(Integer medicine_id) {
        ResultSet rs=tools.query("select * from t_medicine where medicine_id=?",medicine_id);
        Medicine medicines=null;
        try {
            while(rs.next()){
                medicines=new Medicine();
                medicines.setMedicine_id(rs.getInt("medicine_id"));
                medicines.setMedicine_date(rs.getDate("medicine_id").toLocalDate());
                medicines.setMedicine_name(rs.getString("medicine_name"));
                medicines.setMedicine_number(rs.getInt("medicine_number"));
                medicines.setMedicine_price(rs.getDouble("medicine_price"));
                medicines.setMedicine_thd(rs.getInt("medicine_thd"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            tools.clossAll();
        }return medicines;
    }

    @Override
    public boolean deleteProblemSupplierById(Supplier supplier_id) {
        List<Supplier> supplierList=selectAllSupplier();
        for(Supplier supplier:supplierList){
            if(supplier.getSupplier_id()==supplier_id.getSupplier_id()){
                int rs=tools.update("delete from t_supplier where supplier_id=?",supplier_id.getSupplier_id());
                return rs>0;
            }
        }
        return false;
    }
}
